package me.ratel.demo.wechat.entities;

import com.google.common.hash.Hashing;
import me.ratel.demo.eBook.infrastructure.utils.XmlUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Random;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @author Administrator
 * @time 2016/8/28 22:09
 */
@XmlRootElement(name = "xml")
public class OrderQueryRequest {
    private static final Logger log = LoggerFactory.getLogger(OrderQueryRequest.class);

    // String(32)
    // 微信分配的公众账号ID（企业号corpid即为此appId）
    @XmlElement(name = "appid")
    private String appId;

    // String(32)
    // 微信支付分配的商户号
    @XmlElement(name = "mch_id")
    private String mchId;

    // String(32)
    // 微信订单号
    // 微信的订单号，优先使用
    @XmlElement(name = "transaction_id")
    private String transactionId;

    // String(32)
    // 商户系统内部的订单号,32个字符内、可包含字母
    @XmlElement(name = "out_trade_no")
    private String outTradeNo;

    // String(32)
    // 随机字符串，不长于32位
    @XmlElement(name = "nonce_str")
    private String nonceStr;

    // String(32)
    // 签名
    @XmlElement(name = "sign")
    private String sign;

    // API密钥
    private String apiKey;

    private OrderQueryRequest() {
    }

    public OrderQueryRequest(OrderQueryRequestBuilder builder) {
        setAppId(builder.getAppId());
        setMchId(builder.getMchId());

        // 二选一
        setTransactionId(builder.getTransactionId());
        setOutTradeNo(builder.getOutTradeNo());

        setNonceStr(Hashing.md5().hashString(
                String.valueOf(new Random().nextInt(10000)), Charset.forName("UTF-8")
        ).toString());

        setApiKey(builder.getApiKey());

        setSign(createSign());
    }

    private String createSign() {
        // 排序
        SortedMap<String, String> params = new TreeMap<String, String>();
        params.put("appid", getAppId());
        params.put("mch_id", getMchId());
        params.put("transaction_id", getTransactionId());
        params.put("out_trade_no", getOutTradeNo());
        params.put("nonce_str", getNonceStr());

        // 拼装
        StringBuffer sb = new StringBuffer();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (StringUtils.isNoneBlank(value) && !"sign".equals(key))
                sb.append(key + "=" + value + "&");
        }
        sb.append("key=" + getApiKey());

        // 生成签名
        return Hashing.md5().hashString(
                sb.toString(), Charset.forName("UTF-8")
        ).toString().toUpperCase();
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getMchId() {
        return mchId;
    }

    public void setMchId(String mchId) {
        this.mchId = mchId;
    }

    public String getTransactionId() {
        return transactionId;
    }

    public void setTransactionId(String transactionId) {
        this.transactionId = transactionId;
    }

    public String getOutTradeNo() {
        return outTradeNo;
    }

    public void setOutTradeNo(String outTradeNo) {
        this.outTradeNo = outTradeNo;
    }

    public String getNonceStr() {
        return nonceStr;
    }

    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getApiKey() {
        return apiKey;
    }

    public void setApiKey(String apiKey) {
        this.apiKey = apiKey;
    }

    public String toXML() {
        return XmlUtils.toXML(this);
    }
}
